home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15172 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  68 lines

  1. Path: calvin.stemnet.nf.ca!jdeeley
  2. From: jdeeley@calvin.stemnet.nf.ca (J.Deeley)
  3. Newsgroups: comp.lang.c
  4. Subject: bubble sort walkthrough needed, please
  5. Date: Wed, 17 Apr 1996 14:26:02 -0500
  6. Organization: I need to be organized?
  7. Message-ID: <4l39b4$s2h@coranto.ucs.mun.ca>
  8. NNTP-Posting-Host: calvin.stemnet.nf.ca
  9. X-Newsreader: MacSOUP 1.0d7
  10.  
  11. Hi, everyone. I would appreciate your help on this problem that I am
  12. sure you could code in your sleep.
  13.  
  14. I am trying to understand how this bubble sort is working. I understand
  15. the theory, and I understand how the whole thing works except for the
  16. few lines under the comment /* now sort them using a bubble sort */
  17. In other words, it's only the lines 
  18.  
  19. for(a=1; a<count; a++)
  20.                 for(b=count-1; b>=a;--b) 
  21.  
  22. that I can't fathom. I've been working at this ever since last night and
  23. I still don't get it. Could someone tell me what is going on in that
  24. part?
  25.  
  26.  
  27.  
  28. /*this program illustrates the bubble sort*/
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32.  
  33. main ()
  34.  
  35. {
  36.         int item[100];
  37.         int a, b, t;
  38.         int count;
  39.         
  40.         /* read in numbers */
  41.         printf("How many numbers? ");
  42.         scanf("%d", &count);
  43.         for (a=0; a<count; a++) scanf("%d", &item[a]);
  44.         
  45.         /* now sort them using a bubble sort */
  46.         for(a=1; a<count; a++)
  47.                 for(b=count-1; b>=a;--b) {
  48.                         /* compare the adjacent elements */
  49.                                 if(item [b-1] > item [b]) {
  50.                                         /* exchange elements */
  51.                                         t = item [b-1];
  52.                                         item[b-1] = item [b];
  53.                                         item [b] = t;
  54.                                         }
  55.                                 }
  56.                                 
  57.                 /*display sorted list*/
  58.                 for (t=0; t<count; t++) printf("%d", item [t]);
  59.         }
  60.  
  61.  
  62. Thanks!
  63. JD
  64. ---------------------
  65. Caution...Newbie Programmer on Board!
  66. --
  67.  
  68.